package com.ygl.utils;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.Map;

/**
 * @author ygl
 * @date 2021/7/7
 * @description 请求返回值
 */
@Data
public class Result {

    public static Integer SUCCESS_CODE = 200;
    public static Integer ERROR_CODE = 500;
    public Integer status = SUCCESS_CODE;
    public String msg = "操作成功";
    public Object data = null;

    public Result() {
    }

    public Result(Object data) {
        this.data = data;
    }

    public Result(String msg, Object data) {
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer status, String msg) {
        this.msg = msg;
        this.status = status;
    }

    public Result(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    // 成功响应返回

    public static Result ok() {
        return new Result();
    }

    public static Result ok(Object data) {
        return new Result(data);
    }

    public static Map ok(PageInfo pageInfo, String msg) {
        return Maps.build()
                .put("status", SUCCESS_CODE)
                .put("msg", msg)
                .put("list", pageInfo.getList())
                .put("total", pageInfo.getTotal())
                .getMap();
    }

    public static Result ok(String msg, Object data) {
        return new Result(msg, data);
    }

    public static Result ok(Integer status, String msg, Object data) {
        return new Result(status, msg, data);
    }


    // 失败响应返回

    public static Result fail() {
        return new Result(ERROR_CODE, "操作失败");
    }

    public static Result fail(String msg) {
        return new Result(ERROR_CODE, msg, null);
    }

    public static Result fail(Integer status, String msg) {
        return new Result(status, msg, null);
    }
}
